package com.pen.changgou.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pen.changgou.goods.entity.Album;
import com.pen.changgou.goods.service.AlbumService;
import com.pen.changgou.common.core.utils.R;


/**
 * @author yaodao
 * Ddate 2020-11-29 17:35:47
 */
@Api(tags = "相册管理")
@RestController
@RequestMapping("/album")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(albumService.getById(id));
    }

    /**
     * 列表
     */
    @ApiOperation("分页查询(排序)")
    @GetMapping("/page")
    public R<Page> pge( Page page,  Album album) {

//        QueryWrapper<Album> query = Wrappers.query(album);
        QueryWrapper<Album> query = createQueryWrapper(album);
        return R.ok(albumService.page(page, query));

    }

    private QueryWrapper<Album> createQueryWrapper(Album album) {
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(album.getImage())) {
            queryWrapper.lambda().like(Album::getImage, album.getImage());
        }
        if (StringUtils.isNotBlank(album.getImage())) {
            queryWrapper.lambda().like(Album::getImage, album.getImage());
        }
        if (StringUtils.isNotBlank(album.getTitle())) {
            queryWrapper.lambda().like(Album::getTitle, album.getTitle());
        }
        if (StringUtils.isNotBlank(album.getImageItems())) {
            queryWrapper.lambda().like(Album::getImageItems, album.getImageItems());
        }
        return queryWrapper;
    }


    @ApiOperation("分页查询(排序)")
    @GetMapping("/list")
    public R<List<Album>> list(Map<String, Object> param) {
        return R.ok(albumService.list(new QueryWrapper<>()));

    }

    /**
     * 信息
     */
    @ApiOperation("详情查询")
    @GetMapping("/info/{id}")

    public R<Album> info(@PathVariable("id") Long id) {
        Album album = albumService.getById(id);

        return R.ok(album);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    public R<Object> save(@RequestBody Album album) {
        albumService.save(album);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    public R<Object> update(@RequestBody Album album) {

        albumService.updateById(album);

        return R.ok(null);
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
    public R<Object> delete(@RequestBody Long[] ids) {
        albumService.removeByIds(Arrays.asList(ids));

        return R.ok(null);
    }

}
